package fr.inserm.u915.tabixserver;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ConfigErrorFilter implements Filter
		{
		public static final String CONFIG_ERROR="tabix.server.config.error";
		private FilterConfig cfg;
		
		@Override
		public void init(FilterConfig cfg) throws ServletException
			{
			this.cfg=cfg;
			}
		
		@Override
		public void destroy()
			{
			this.cfg=null;
			}
	
		@Override
		public void doFilter(
				ServletRequest req, ServletResponse res,
				FilterChain chain) throws IOException, ServletException
			{
			Throwable err=(Throwable)cfg.getServletContext().getAttribute(CONFIG_ERROR);
			if(err==null)
				{
				chain.doFilter(req, res);
				return;
				}
			err.printStackTrace();
			HttpServletRequest request=(HttpServletRequest)req;
			request.getRequestDispatcher("/WEB-INF/jsp/config-error.jsp").forward(req, res);
			}
	
	
		}
